<?php
/**
 * 建造者模式
 */
abstract class Builder
{
   protected $car;
   abstract public function  buildPartA();
   abstract public function  buildPartB();
   abstract public function  buildPartC();
   abstract public function  getResult();
}

class Car
{
    protected $partA;
    protected $partB;
    protected $partC;
    
    public function setPartA($str){
        $this->partA = $str;
    }
    
    public function setPartB($str){
        $this->partB = $str;
    }
    
    public function setPartC($str){
        $this->partC = $str;
    }
    
    public function show()
    {
        echo "这辆车由：".$this->partA.','.$this->partB.',和'.$this->partC.'组成';
    }
}


class CarBuilder extends Builder
{
    public function __construct()
    {
        $this->car = new Car();
    }
    
    public function buildPartA()
    {
        $this->car->setPartA("发动机");
    }
    public function buildPartB()
    {
        $this->car->setPartB("轮子");
    }
    public function buildPartC()
    {
        $this->car->setPartC("其他零件");
    }
    public function getResult()
    {
        return $this->car;
    }
}

class Director
{
    public $my_builder;
    
    public function startBuild()
    {
        $this->my_builder->buildPartA();
        $this->my_builder->buildPartB();
        $this->my_builder->buildPartC();
        return $this->my_builder->getResult();
    }
    
    public function setBuilder(Builder $builder)
    {
        $this->my_builder = $builder;
    }
}

$car_builder = new CarBuilder();
$director = new Director();
$director->setBuilder($car_builder);
$new_car = $director->startBuild();
$new_car->show();
